<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="模版名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入模版名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="模版范围" prop="scope">
        <el-select v-model="queryParams.scope" placeholder="请选择模版范围" clearable size="small">
          <el-option
            v-for="dict in scopeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="模版类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择模版类型" clearable size="small">
          <el-option
            v-for="dict in typeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['dms:non_drug_model:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['dms:non_drug_model:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['dms:non_drug_model:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['dms:non_drug_model:export']"
        >导出
        </el-button>
      </el-col>
      <div class="top-right-btn">
        <el-tooltip class="item" effect="dark" content="刷新" placement="top">
          <el-button size="mini" circle icon="el-icon-refresh" @click="handleQuery"/>
        </el-tooltip>
        <el-tooltip class="item" effect="dark" :content="showSearch ? '隐藏搜索' : '显示搜索'" placement="top">
          <el-button size="mini" circle icon="el-icon-search" @click="showSearch=!showSearch"/>
        </el-tooltip>
      </div>
    </el-row>

    <el-table v-loading="loading" :data="non_drug_modelList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="模版编号" align="center" prop="id"/>
      <el-table-column label="模版名称" align="center" prop="name"/>
      <el-table-column label="模版范围" align="center" prop="scope" :formatter="scopeFormat"/>
      <el-table-column label="模版简介" align="center" prop="aim"/>
      <el-table-column label="模版编码" align="center" prop="code"/>
      <el-table-column label="模版类型" align="center" prop="type" :formatter="typeFormat"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['dms:non_drug_model:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['dms:non_drug_model:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog :title="title" :visible.sync="open" append-to-body width="80%">
      <addNonDrugModel @closeOpen="closeOpen" ref="addNonDrugModel" v-show="addShow"></addNonDrugModel>
      <editNonDrugModel :id="id" @closeOpen="closeOpen" ref="editNonDrugModel" v-show="editShow"></editNonDrugModel>
    </el-dialog>
  </div>
</template>

<script>
  import {delNon_drug_model, exportNon_drug_model, listNon_drug_model} from "@/api/dms/non_drug_model";
  import addNonDrugModel from "../non_drug_model/add_non_drug_model"
  import editNonDrugModel from "../non_drug_model/edit_non_drug_model"

  export default {
    name: "Non_drug_model",
    components: {addNonDrugModel, editNonDrugModel},
    data() {
      return {
        // 遮罩层
        loading: true,
        //选中id
        id: null,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 非药品模版表格数据
        non_drug_modelList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        //新增弹出框是否显示
        addShow: false,
        //修改新增框是否显示
        editShow: false,
        // 模版状态字典
        statusOptions: [],
        // 模版范围字典
        scopeOptions: [],
        // 模版类型字典
        typeOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          name: null,
          nonDrugIdList: null,
          scope: null,
          aim: null,
          code: null,
          type: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {}
      };
    },
    created() {
      this.getList();
      this.getDicts("model_status").then(response => {
        this.statusOptions = response.data;
      });
      this.getDicts("model_scope").then(response => {
        this.scopeOptions = response.data;
      });
      this.getDicts("no_drug_type").then(response => {
        this.typeOptions = response.data;
      });
    },
    methods: {
      /** 查询非药品模版列表 */
      getList() {
        this.loading = true;
        listNon_drug_model(this.queryParams).then(response => {
          this.non_drug_modelList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 模版状态字典翻译
      statusFormat(row, column) {
        return this.selectDictLabel(this.statusOptions, row.status);
      },
      // 模版范围字典翻译
      scopeFormat(row, column) {
        return this.selectDictLabel(this.scopeOptions, row.scope);
      },
      // 模版类型字典翻译
      typeFormat(row, column) {
        return this.selectDictLabel(this.typeOptions, row.type);
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          status: 0,
          name: null,
          nonDrugIdList: null,
          scope: null,
          ownId: null,
          aim: null,
          createTime: null,
          code: null,
          type: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        if (this.editShow) {
          this.editShow = !this.editShow;
        }
        this.reset();
        this.addShow = true;
        this.open = true;
        this.title = "添加非药品收费项目";
        this.$refs.addNonDrugModel.$createElement();
      },
      /** 修改按钮操作 */
      async handleUpdate(row) {
        if (this.addShow) {
          this.addShow = !this.addShow;
        }
        this.reset();
        this.id = row.id;
        this.editShow = true;
        this.open = true;
        this.title = "修改非药品收费项目";
        // FIXED BUG_019
        setTimeout(()=>{
          this.$refs.editNonDrugModel.initialize(this.id);
        }, 100)
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm('是否确认删除非药品模版编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return delNon_drug_model(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function () {
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有非药品模版数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return exportNon_drug_model(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function () {
        });
      },
      closeOpen(data) {
        //alert(data);
        this.open = data;
      }
    }
  }
</script>
